Completed
Push — master ( 88fa0f...de94f8 )
by greg
01:51
created

Builder.js ➔ ... ➔ build   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
c 0
b 0
f 0
nc 6
dl 0
loc 21
rs 9.0534
nop 1
1
import fse from 'fs-extra'
2
import {saveHtml} from './cms/operations/save'
3
import path from 'path'
4
5
import {
6
  coreUtils,
7
  cmsData,
8
  config,
9
  fileAttr,
10
  cmsTemplates,
11
  Page
12
} from './'
13
14
class Builder {
15
16
  constructor(root, folder, dest, flow){
17
    const dataExtension = '.json'
18
    const templateExtension = '.' + config.files.templates.extension
19
    const pathToJson = path.join(root, config.data.url)
20
21
    let files = fileAttr.filterLatestVersion(coreUtils.file.getFilesSync(this.pathToJson, true, dataExtension), flow)
22
23
    if(flow === 'publish') {
24
      files = coreUtils.file.getFilesSync(path.join(root, config.publish.url), true, templateExtension)
25
    }
26
27
    const build = function (index) {
28
      let file = files[index]
29
      if(file.indexOf(templateExtension) > -1){
30
        file = file.replace(config.publish.url, config.data.url).replace(templateExtension, dataExtension)
31
      }
32
      if(file.indexOf(dataExtension) > -1){
33
        const json = fse.readJsonSync(file)
34
        const text = cmsTemplates.template.getTemplate(json.abe_meta.template)
35
        
36
        cmsData.source.getDataList(path.dirname(json.abe_meta.link), text, json)
37
          .then(() => {
38
            var page = new Page(json.abe_meta.template, text, json, true)
39
            saveHtml(path.join(root, dest + json.abe_meta.link), page.html)
40
            if(files[index + 1]) build(index + 1)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
41
          }).catch(function(e) {
42
            console.error(e)
43
            if(files[index + 1]) build(index + 1)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
44
          })
45
      }
46
      else if(files[index + 1]) build(index + 1)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
47
    }
48
49
    build(0)
50
  }
51
}
52
53
if(process.env.ROOT && process.env.FOLDER && process.env.DEST){
54
  config.set({root: process.env.ROOT})
55
  var dest = process.env.DEST || 'tmp'
56
  var flow = process.env.FLOW || 'draft'
57
  new Builder(process.env.ROOT, process.env.FOLDER, dest, flow)
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new Builder(process.env....env.FOLDER, dest, flow) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
58
}
59